<template>
  <el-dialog :visible="dialogVisible" title="新增权限点" @close="close">
    <el-form ref="addPerFrom" :model="addPerFrom" :rules="rules" label-width="120px">
      <el-form-item prop="name" label="权限名称" style="width:85%">
        <el-input v-model="addPerFrom.name" />
      </el-form-item>
      <el-form-item prop="code" label="权限标识" style="width:85%">
        <el-input v-model="addPerFrom.code" />
      </el-form-item>
      <el-form-item prop="description" label="权限描述" style="width:85%">
        <el-input v-model="addPerFrom.description" />
      </el-form-item>
      <el-form-item label="开启">
        <el-switch v-model="addPerFrom.enVisible" active-value="1" inactive-value="0" />
      </el-form-item>
      <el-form-item style="width:85%">
        <el-row type="flex" justify="center">
          <el-button type="primary" @click="addpermClick">确定</el-button>
          <el-button @click="close">取消</el-button>
        </el-row>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { addPermissionListAPI, editPermissionAPI, lookPermissionAPI } from '@/api/permission'
export default {
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    ids: {
      type: Number || null,
      default: null
    },
    types: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      addPerFrom: {
        name: '',
        code: '',
        description: '',
        enVisible: '1',
        type: 0,
        pid: 0
      },
      rules: {
        name: [{ required: true, message: '权限名为空', tergger: 'blur' }],
        code: [{ required: true, message: '权限名标志为空', tergger: 'blur' }]
      }
    }
  },
  methods: {
    close() {
      this.$emit('update:dialogVisible', false)
      this.$refs.addPerFrom.resetFields()
    },
    async lookPermission() {
      this.addPerFrom = await lookPermissionAPI(this.ids)
    },
    addpermClick() {
      this.$refs.addPerFrom.validate(async(isOK) => {
        if (isOK) {
          if (this.types === 'add1') {
            await addPermissionListAPI({ ...this.addPerFrom, type: 1, pid: 0 })
            this.$message.success('添加成功')
          }
          if (this.types === 'add2') {
            await addPermissionListAPI({ ...this.addPerFrom, type: 2, pid: this.ids })
            this.$message.success('添加成功')
          }
          if (this.types === 'edit') {
            await editPermissionAPI(this.addPerFrom)
            this.$message.success('修改成功')
          }
          this.$nextTick(() => {
            this.$emit('getPermissionList')
            this.close()
          })
        }
      })
    }

  }
}
</script>

<style>

</style>
